package net.airtheva.uniqueTask4.fragment;

import net.airtheva.uniqueTask4.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;

public class MediaFragment extends Fragment {

	class _OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			Switch();
		}
		
	}
	
	Switch mSwitch;
	
	Fragment mFileFragment;
	Fragment mStoreFragment;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_media, null);
		mSwitch = (Switch) view.findViewById(R.id.fragment_media_switch);
		mSwitch.setOnCheckedChangeListener(new _OnCheckedChangeListener());
		
		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Switch();
	}
	
	void Switch() {
		
		if(mSwitch.isChecked()) {
			
			if(mFileFragment == null) {
				mFileFragment = new FileFragment();
			}
			
			getChildFragmentManager().beginTransaction()
			.replace(R.id.fragment_media_container, mFileFragment)
			.commit();
		}
		else {
			
			if(mStoreFragment == null) {
				mStoreFragment = new StoreFragment();
			}
			
			getChildFragmentManager().beginTransaction()
			.replace(R.id.fragment_media_container, mStoreFragment)
			.commit();
		}
		
	}

}
